<?php
/*
*************kuozhan扩展页面*******************
*****404错误页、快速查询工具页、*********
*/
class KuozhanApp extends MallbaseApp
{
	//404错误提示页
	function the404()
    {
        /* 配置seo信息 */
		$this->_config_seo(array(
            'title'       => "404页面,您查看的页面不存在或已迁移_" . Conf::get('site_name'),
            'keywords'    => "404页面,404错误页面,404错误",
            'description' => "这里是道达快递404错误页面,您查看的页面由于各种问题导致页面不存在或者已迁移,请点击其他链接查看更精彩的内容"
        ));		
		$this->display('kuozhan.the404.html');
	}
	//快速查询工具
    function kuaidi()
    {
		$this->display('kuozhan.kuaidi.html');
	}	
    //------------------------------根据订单号批量查询运费---查询页面--------------------------------
	function freight()
	{
		if (IS_POST)
		{
			$order_text = isset($_POST['order_text']) ? $_POST['order_text'] : '';
			$ordertext = isset($_POST['ordertext']) ? $_POST['ordertext'] : '';
			$order_post['baseweight'] = isset($_POST['baseweight']) ? intval($_POST['baseweight']) : 0;
			$order_post['overweight'] = isset($_POST['overweight']) ? intval($_POST['overweight']) : 0;
			
			if (!$order_text && !$ordertext)
			{
				$this->show_warning('请输入订单编号');
				return;
			}
			if ( $order_post['baseweight'] <= 0 || $order_post['overweight'] <= 0)
			{
				$this->show_warning('首重费用或续重费用必须大于零');
				return;
			}	
			//判断哪个鬼方式进来
			if ( $ordertext )
			{
				$order_post['order_text'] = implode(',',$ordertext);
			}else{
				$order_post['order_text'] = $this->htmlencode($order_text);
			}
			$order_mod =& m('order');
			$order_goods_mod =& m('ordergoods');
			$order_info = $order_mod->getAll("SELECT order_id FROM {$order_mod->table} WHERE order_sn IN({$order_post['order_text']})");
			if ( !$order_info )
			{
				$this->show_warning('没有搜索到相当订单');
				return;
			}
			foreach ($order_info as $key => $orderinfo)
			{
				$order_implode .= $orderinfo['order_id'].',';
			}
			$order_implode = rtrim($order_implode,',');
			$order_post['order_weight'] = $order_goods_mod->getOne("SELECT sum(weight) FROM {$order_goods_mod->table} WHERE order_id IN({$order_implode})");
			
			//开始计算总运费出来
			if($order_post['order_weight']-1 > 0){
				$order_post['order_freight'] = $order_post['baseweight'] + $order_post['overweight'] * ($order_post['order_weight']-1);
			}else{
				$order_post['order_freight'] = $order_post['baseweight'];
			}
		}
		$this->assign('order_post', $order_post);		
		$this->display('kuozhan.freight.html');
	}
    function htmlencode($fstring)  
	{ 
	   if($fstring)
	   {		  
		 $fstring = str_replace("&gt;",">",$fstring);
		 $fstring = str_replace("<","&lt;",$fstring);   
		 $fstring = str_replace(chr(32),"",$fstring);   
		 $fstring = str_replace(chr(9),"",$fstring);   
		 $fstring = str_replace(chr(34),"",$fstring);   
		 $fstring = str_replace(chr(39),"",$fstring);  
		 $fstring = str_replace(chr(10)&chr(10),"",$fstring);  
		 $fstring = str_replace(chr(10),"",$fstring);
		 $fstring = str_replace(chr(13),",",$fstring);
		 $fstring = explode(',',$fstring);
		 foreach ($fstring as $key => $fstrings)
		 {
			 $fstring_s .= intval($fstrings).',';
		 }
		 $fstring_s =  rtrim($fstring_s,',');
		 return $fstring_s; 
	   }
    }
    //------------------------------根据订单号批量查询运费---查询页面--------------------------------
	/* 订单打印系统操作 */
	function OrderPrint()
	{
		if (IS_POST){
			$ordertext = isset($_POST['ordertext']) ? $_POST['ordertext'] : '';
		}else{
			$ordertext = isset($_GET['ordertext']) ? $_GET['ordertext'] : '';
		}
		if (!$ordertext)
		{
			$this->show_warning('请选择需要打印的订单编号');
			return;
		}
		$order_mod =& m('order');
		$order_info = $order_mod->findAll(array(
            'fields'        => "order_alias.*,order_alias.*,order_extm.*,s.owner_name,s.store_name,s.tel,s.return_address",
			'conditions'    => "order_sn ".db_create_in($ordertext),
            'count'         => true,
            'join'          => 'has_orderextm,belongs_to_store',
            'order'         => 'order_alias.add_time DESC',
            'include'       =>  array( 'has_ordergoods'),//取出商品
        ));
		if ( !$order_info )
		{
			$this->show_warning('没有找到到相关的记录');
			return;
		}
        $model_goods =& m('goods');
		$data_info = array();  $data_send = array();  $data_address = array();
		foreach( $order_info as $key => $orderinfo)
		{
			$order_info[$key]['freight'] = round ( $orderinfo['order_amount']-$orderinfo['goods_amount'],3);
			$region_name = str_replace("	","",$orderinfo['region_name']); 
			$order_info[$key]['region_name'] = $region_name;
			/* 查出最新的相应的货号 */
		    $goods_ids = array();foreach ($orderinfo['order_goods'] as $keyy => $goodslist){ $goods_ids[] = $goodslist['goods_id']; }
			$goods_info = $model_goods->find(array( 'conditions' => 'goods_id' . db_create_in($goods_ids), 'fields' => 'kuanhao', ));
			foreach( $orderinfo['order_goods'] as $keys => $goodslists)
			{
				$order_info[$key]['quantity_total'] += $goodslists['quantity'];
				$order_info[$key]['weight_total'] += $goodslists['weight'];
				$order_info[$key]['order_goods'][$keys]['sku'] = $goods_info[$goodslists['goods_id']]['kuanhao'];
				$order_info[$key]['order_goods'][$keys]['subtotal'] += $goodslists['price'] * $goodslists['quantity'];;	
			}
			//件数、重量、运费、总金额---------------------------------------------
			$data_info['quantity_total'] += $order_info[$key]['quantity_total'];
			$data_info['weight_total'] += $order_info[$key]['weight_total'];
			$data_info['freight_total'] += $order_info[$key]['freight'];
			$data_info['amount_total'] += $orderinfo['order_amount'];
			//寄件人信息js转------------------------------------------------------
			$data_send[$orderinfo['order_id']] = array(
			     'send_company' => $orderinfo['seller_name'],
				 'send_name' => $orderinfo['consignee'],
				 'send_tel' => $orderinfo['tel'],
				 'send_address' => $orderinfo['return_address'],
			);
			//收件人信息js转------------------------------------------------------
			$data_address[$orderinfo['order_id']] = array( 
			     'region_id' => $orderinfo['region_id'],
				 'consignee' => $orderinfo['consignee'],
				 'region_name' => $orderinfo['region_name'],
				 'address' => $region_name.'-'.$orderinfo['address'],
				 'zipcode' => $orderinfo['zipcode'],
				 'phone_tel' => $orderinfo['phone_mob'].','.$orderinfo['phone_tel'],
			);
		}
		$data_info = array(
		    'quantity_total' => $data_info['quantity_total'],
			'weight_total' => round($data_info['weight_total']),
			'freight_total' => round($data_info['freight_total']),
			'amount_total' => round($data_info['amount_total'])
		 );
		$data_send = ecm_json_encode($data_send);
		$data_address = ecm_json_encode($data_address);
		$this->assign('data_info', $data_info);
		$this->assign('data_send', $data_send);
		$this->assign('data_address', $data_address);	
		
		$this->assign('ordertext', is_array($ordertext) ? implode(',',$ordertext) : $ordertext );
		$this->assign('order_info', $order_info);		
		$this->display('kuozhan.OrderPrint.html');
	}
	
	/* 道达快递：快递运单面单打印 index.php?app=my_kdorder */
	function KdOrderPrint()
	{
		if (IS_POST){
			$id = isset($_POST['id']) ? $_POST['id'] : '';
		}else{
			$id = isset($_GET['id']) ? $_GET['id'] : '';
		}
		if (!$id){ $this->show_warning('请选择需要打印的订单编号'); return; }
		
		$kd_order_mod =& m('kd_order');
		$kd_yubao_mod =& m('kd_yubao');
		$order_info = $kd_order_mod->findAll(array(
			'conditions'    => "order_alias.order_id ".db_create_in($id),
            'count'         => true,
            'join'          => 'has_kdorderextm',
            'order'         => 'order_alias.addtime DESC',
        ));
		if ( !$order_info ){ $this->show_warning('没有找到到相关的记录'); return; }
		
		foreach ($order_info as $key => $list){ 
		  $countrys1 = explode('//',$list['txt_countrys']);
		  $order_info[$list['order_id']]['txt_country'] = $countrys1[0];
		  $order_info[$list['order_id']]['txt_province'] = $countrys1[1];
		  $order_info[$list['order_id']]['txt_city'] = $countrys1[2];
		  $countrys = explode('//',$list['txtRece_countrys']);
		  $order_info[$list['order_id']]['txtRece_country'] = $countrys[0];
		  $order_info[$list['order_id']]['txtRece_province'] = $countrys[1];
		  $order_info[$list['order_id']]['txtRece_city'] = $countrys[2];	
		  $order_info[$list['order_id']]['txtqu'] = $countrys[3];
		  $order_info[$list['order_id']]["goodswupin"] = $kd_yubao_mod->find("order_id=".$list['order_id']); 
		}
		$this->assign('order_info', $order_info);		
				
		$this->assign('ordertext', is_array($id) ? implode(',',$id) : $id );
		$this->display('kuozhan.KdOrderPrint.html');			
	}
	/* 道达快递：打印合作客户的条形码 index.php?app=kd_order&act=kehu_print */
	function KehuPrint()
	{
		if (IS_POST){
			$id = isset($_POST['id']) ? $_POST['id'] : '';
		}else{
			$id = isset($_GET['id']) ? $_GET['id'] : '';
		}
		if (!$id){ $this->show_warning('请选择需要打印的条形码'); return; }

		$this->assign('select_list', array( 'print'=> $_REQUEST['print'], 'number'=> $_REQUEST['number']) );
		
		$this->assign('data_info', $id);			
				
		$this->display('kuozhan.KehuPrint.html');
	}
}
?>